/*
* pps_bom
*/
package com.pitop.project.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.project.entity.Project;
import com.pitop.project.service.ProjectSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/project")
@Slf4j
@Api(value = "项目控制器", description = "项目控制器")
public class ProjectCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private ProjectSV projectSV;

    /**
     * 查询Project一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public Project load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Project project = projectSV.load(id, code);
        log.info(JSON.toJSONString(project));
        return project;
    }

    /**
     * 查询Project一个详情信息，(将查询关联数据)
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public Project get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Project project = projectSV.get(id, code);
        log.info(JSON.toJSONString(project));
        return project;
    }

    /**
     * 根据条件id查询Project一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public Project loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Project project = projectSV.loadById(id);
        log.info(JSON.toJSONString(project));
        return project;
    }

    /**
     * 根据条件code查询Project一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public Project loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Project project = projectSV.loadByCode(code);
        log.info(JSON.toJSONString(project));
        return project;
    }

    /**
     * 根据条件name查询Project一个详情信息
     *
     * @param name 项目编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "项目编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/name/{name}")
    @ResponseBody
    public Project loadByName(@PathVariable String name) {
        if (name == null) {
            return null;
        }
        Project project = projectSV.loadByName(name);
        log.info(JSON.toJSONString(project));
        return project;
    }

    /**
     * 根据条件查询Project一个详情信息
     *
     * @param projectCode 项目代码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目代码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/projectCode/{projectCode}")
    @ResponseBody
    public Project loadByProjectCode(@PathVariable String projectCode) {
        if (projectCode == null) {
            return null;
        }
        Project project = projectSV.loadByProjectCode(projectCode);
        log.info(JSON.toJSONString(project));
        return project;
    }

    /**
     * 根据条件id查询Project一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public Project getById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Project project = projectSV.getById(id);
        log.info(JSON.toJSONString(project));
        return project;
    }

    /**
     * 根据条件code查询Project一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public Project getByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Project project = projectSV.getByCode(code);
        log.info(JSON.toJSONString(project));
        return project;
    }

    /**
     * 查询Project信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Project信息集合", notes = "查询Project信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCategoryCode", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "自己级别", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "项目名", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目代码", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "项目说明", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态[开发Develop，稳定Stable，停用 Stop]", paramType = "query"),
            @ApiImplicitParam(name = "operateCode", value = "创建人编码", paramType = "query"),
            @ApiImplicitParam(name = "operateName", value = "创建人", paramType = "query"),
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<Project> list(@RequestBody @ApiIgnore Project project, Integer curPage, Integer pageSize) {
        Page<Project> page = new Page<Project>(pageSize, curPage);
        List<Project> projects = projectSV.list(project, page.genRowStart(), page.getPageSize());
        int total = projectSV.count(project);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return projects;
    }


    /**
     * 统计Project信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Project信息数量", notes = "统计Project信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCategoryCode", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "自己级别", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "项目名", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目代码", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "项目说明", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态[开发Develop，稳定Stable，停用 Stop]", paramType = "query"),
            @ApiImplicitParam(name = "operateCode", value = "创建人编码", paramType = "query"),
            @ApiImplicitParam(name = "operateName", value = "创建人", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody Project project) {
        return projectSV.count(project);
    }

    /**
     * 创建Project
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Project", notes = "创建Project")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "项目名", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目代码", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "项目说明", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
            @ApiImplicitParam(name = "operateCode", value = "创建人编码", paramType = "query"),
            @ApiImplicitParam(name = "operateName", value = "创建人", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public Project build(@RequestBody @ApiIgnore Project project) {
        projectSV.save(project);
        return project;
    }

    /**
     * 修改Project
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Project", notes = "修改Project")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "项目名", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目代码", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "项目说明", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
    })
    @PutMapping("/modify")
    @ResponseBody
    public Project modify(@RequestBody @ApiIgnore Project project) {
        projectSV.modify(project);
        return project;
    }

    /**
     * 删除Project
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Project", notes = "删除Project")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        projectSV.delete(id, code);
        return BeanRet.create(true, "删除Project成功");
    }

    /**
     * 修改项目状态
     *
     * @param code  编码
     * @param state 状态
     */
    @ApiOperation(value = "修改项目状态", notes = "修改项目状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, String state) {
        projectSV.updateByCode(code, state);
        return BeanRet.create(true, "修改成功");
    }

    /**
     * copy項目
     *
     * @return BeanRet
     */
    @ApiOperation(value = "copy項目", notes = "copy項目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "account", value = "账号", paramType = "query"),
            @ApiImplicitParam(name = "accountCode", value = "账户编码", paramType = "query"),
    })
    @GetMapping("/copy")
    @ResponseBody
    public BeanRet copy(String code, String account, String accountCode) {
        if ( StringUtils.isEmpty(code) || StringUtils.isEmpty(account) || StringUtils.isEmpty(accountCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        projectSV.copy(code, account, accountCode);
        return BeanRet.create(true, "copy項目成功");
    }
}
